//
//  DiscoverViewController.m
//  News
//
//  Created by 徐涵菲 on 2018/4/23.
//  Copyright © 2018年 Wangly. All rights reserved.
//

#import "DiscoverViewController.h"
#import "SearchBar.h"

@interface DiscoverViewController ()

/**
 *  搜索框
 */
@property (nonatomic, weak) SearchBar *searchBar;
@end

@implementation DiscoverViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    // 添加搜索框
    SearchBar *searchBar = [[SearchBar alloc] init];
    searchBar.frame = CGRectMake(0, 0, 300, 30);
    self.navigationItem.titleView = searchBar;
    
    searchBar.delegate=self;
    self.searchBar=searchBar;
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
    // 1.添加按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancle)];
    
    // 2.替换leftView
    self.searchBar.leftIconName = @"settings_statistic_triangle";
    
}

- (void)cancle
{
    DDActionLog;
    
    // 0.清除取消按钮
    self.navigationItem.rightBarButtonItem = nil;
    // 1.关闭键盘
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    // 2.还原leftView
    self.searchBar.leftIconName = @"searchbar_searchlist_search_icon";
}

#pragma mark - 监听scorllerView的拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 1.关闭键盘
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}


@end
